package com.handpay.nocardpay.baselibrary.main.mine.presenter;

import android.content.Context;

import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseResponseBean;
import com.handpay.nocardpay.baselibrary.commbase.presenter.IBasePresenter;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.lottery.model.ILotteryStatusModel;
import com.handpay.nocardpay.baselibrary.lottery.model.bean.LotteryStatus;
import com.handpay.nocardpay.baselibrary.lottery.model.impl.LotteryStatusModelImpl;
import com.handpay.nocardpay.baselibrary.lottery.view.ILotteryView;
import com.handpay.nocardpay.baselibrary.main.mine.view.IMerchInfoView;
import com.handpay.nocardpay.baselibrary.main.mine.view.ILimitView;
import com.handpay.nocardpay.baselibrary.main.mine.model.ILimitModel;
import com.handpay.nocardpay.baselibrary.main.mine.model.IMerchInfoModel;
import com.handpay.nocardpay.baselibrary.main.mine.model.bean.DayLimit;
import com.handpay.nocardpay.baselibrary.main.mine.model.bean.MerchInfo;
import com.handpay.nocardpay.baselibrary.main.mine.model.impl.LimitModelImpl;
import com.handpay.nocardpay.baselibrary.main.mine.model.impl.MerchInfoModelImpl;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/14.
 */

public class MinePresenterImpl implements IBasePresenter {
    private Context mContext;
    private ILimitModel<DayLimit> limitModel;
    private ILimitView mLimitView;
    private IMerchInfoModel<MerchInfo,BaseResponseBean> merchInfoModel;
    private IMerchInfoView iMerchInfoView;

    private ILotteryView lotteryView;
    private ILotteryStatusModel<LotteryStatus> mLotteryStatusModel;

    public void setLotteryView(ILotteryView lotteryView) {
        this.lotteryView = lotteryView;
    }

    public void setmLimitView(ILimitView mLimitView) {
        this.mLimitView = mLimitView;
    }

    public void setiMerchInfoView(IMerchInfoView iMerchInfoView) {
        this.iMerchInfoView = iMerchInfoView;
    }

    public MinePresenterImpl(Context context) {
        this.mContext = context;
        limitModel = new LimitModelImpl(mContext);
        merchInfoModel = new MerchInfoModelImpl(mContext);
        mLotteryStatusModel = new LotteryStatusModelImpl(mContext);
    }

    /**
     * 查询每日限额
     *
     * @param paramers
     * @param isShowLoading
     */
    public void queryLimit(HashMap<String, Object> paramers, boolean isShowLoading) {
        if (mLimitView != null) {
            if (isShowLoading)
                mLimitView.showProgress();
        }
        limitModel.queryDayLimit(paramers, DayLimit.class, new IBaseModel.OnRequestFinishedListener<DayLimit>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (mLimitView != null) {
                    mLimitView.hideProgress();
                    mLimitView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(DayLimit response) {
                if (mLimitView != null) {
                    mLimitView.hideProgress();
                    mLimitView.querySuccess(response);
                }
            }
        });
    }

    /**
     * 查询当前账户信息
     *
     * @param paramers
     * @param clzz
     * @param isShowLoading
     */
    public void getMerchInfo(HashMap<String, Object> paramers, Class<MerchInfo> clzz, boolean isShowLoading) {
        if (iMerchInfoView != null) {
            if (isShowLoading)
                iMerchInfoView.showProgress();
        }
        merchInfoModel.getMerchInfo(paramers, clzz, new IBaseModel.OnRequestFinishedListener<MerchInfo>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (iMerchInfoView != null) {
                    iMerchInfoView.hideProgress();
                    iMerchInfoView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(MerchInfo response) {
                if (iMerchInfoView != null) {
                    iMerchInfoView.hideProgress();
                    iMerchInfoView.querySuccess(response);
                }
            }
        });
    }

    /**
     * 商户统计,不需要处理回调
     * @param paramers
     */
    public void submitMarchData(HashMap<String, Object> paramers){
        if (merchInfoModel!=null){
            merchInfoModel.submitMerchStatistical(paramers, BaseResponseBean.class, new IBaseModel.OnRequestFinishedListener<BaseResponseBean>() {
                @Override
                public void onError(BaseErrorInfo baseErrorInfo) {
                    HPLog.e(HPLog.LFP_TAG,"submitMarchData->onError:"+baseErrorInfo.toString());
                }

                @Override
                public void onSuccess(BaseResponseBean response) {
                    HPLog.i(HPLog.LFP_TAG,"submitMarchData->onSuccess");
                }
            });
        }
    }
    /**
     * 查询抽奖开关
     *
     * @param paramers
     * @param isShowLoading
     */
    public void queryLotteryStatus(HashMap<String, Object> paramers, boolean isShowLoading) {
        if (lotteryView != null) {
            if (isShowLoading) {
                lotteryView.showProgress();
            }
            mLotteryStatusModel.getLotteryStatus(paramers, LotteryStatus.class, new IBaseModel.OnRequestFinishedListener<LotteryStatus>() {
                @Override
                public void onError(BaseErrorInfo baseErrorInfo) {
                    if (lotteryView != null) {
                        lotteryView.hideProgress();
//                        lotteryView.showErrorAlert(baseErrorInfo);
                    }
                }

                @Override
                public void onSuccess(LotteryStatus response) {
                    if (lotteryView != null) {
                        lotteryView.hideProgress();
                        lotteryView.queryLotteryStatusFinsh(response);
                    }
                }

            });
        }

    }

    @Override
    public void onDestory() {
        if (mLimitView != null)
            mLimitView = null;
        if (iMerchInfoView != null)
            iMerchInfoView = null;
        if (lotteryView != null)
            lotteryView = null;
        if (limitModel!=null)
            limitModel=null;
        if (merchInfoModel!=null)
            merchInfoModel=null;
        if(mLotteryStatusModel!=null)
            mLotteryStatusModel=null;

    }
}
